package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import javax.xml.datatype.DatatypeFactory;  
import javax.xml.datatype.XMLGregorianCalendar;  
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Date; 
import com.lb.jwitter.service.ConditionReport; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.util.Message; 
import java.util.Collections; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import java.util.List; 
import java.util.Map; 
import com.lb.jwitter.util.ApplicationContextProvider; 
 import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.lb.jwitter.service.TblThongTinHocVienMonHoc;
import com.lb.jwitter.service.TblThongTinHocVienMonHocService;

import com.lb.jwitter.service.TblThongTinHocVien;
import com.lb.jwitter.service.TblThongTinHocVienService;

import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblThongTinHocVienMonHocAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private String idList; 
    public String getIdList() { return idList; } 
    public void setIdList(String idList) { this.idList = idList; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public Long getId() {
       return id;
 }

    public void setId(Long id) {
       this.id = id;
 }

    private int hocVienId; 
    public void setHocVienId(int hocVienId) {
       this.hocVienId = hocVienId;
 }

    public int getHocVienId() {
       return hocVienId;
 }

    private int monHocId; 
    public void setMonHocId(int monHocId) {
       this.monHocId = monHocId;
 }

    public int getMonHocId() {
       return monHocId;
 }

    private String trangThai; 
    public void setTrangThai(String trangThai) {
       this.trangThai = trangThai;
 }

    public String getTrangThai() {
       return trangThai;
 }

    private TblThongTinHocVienMonHoc tblThongTinHocVienMonHoc; 
    public TblThongTinHocVienMonHoc getTblThongTinHocVienMonHoc () {
        return this.tblThongTinHocVienMonHoc ;
 }

    private Collection<TblThongTinHocVienMonHoc> tblThongTinHocVienMonHocs; 
    public Collection<TblThongTinHocVienMonHoc> getTblThongTinHocVienMonHocs() {
        return this.tblThongTinHocVienMonHocs; 
      } 

    private TblThongTinHocVienMonHocService tblThongTinHocVienMonHocService; 

    private Collection<TblThongTinHocVien> tblThongTinHocViens; 
    public Collection<TblThongTinHocVien> getTblThongTinHocViens() {
         return this.tblThongTinHocViens; 
    }

    private TblThongTinHocVienService tblThongTinHocVienService; 

    private Collection<TblCTDTMonHoc> tblCTDTMonHocs; 
    public Collection<TblCTDTMonHoc> getTblCTDTMonHocs() {
         return this.tblCTDTMonHocs; 
    }

    private TblCTDTMonHocService tblCTDTMonHocService; 

	private int preUpdate(Message msg, TblThongTinHocVienMonHoc obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postUpdate(Message msg, TblThongTinHocVienMonHoc obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String update() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_ThongTin_HocVien_MonHoc", "sua")){ 
        return "noprivilege"; 
    } 
    tblThongTinHocVienService = (TblThongTinHocVienService)context.getBean("tbl_ThongTin_HocVienClient"); 

    tblCTDTMonHocService = (TblCTDTMonHocService)context.getBean("tbl_CTDT_MonHocClient"); 
          tblThongTinHocVienMonHocService = (TblThongTinHocVienMonHocService)context.getBean("tbl_ThongTin_HocVien_MonHocClient"); 
        
          
          msg = new Message(); 
        tblThongTinHocVienMonHoc = tblThongTinHocVienMonHocService.gettblThongTinHocVienMonHoc(new Long(id)); 
        tblThongTinHocVienMonHoc.setHocVienId(tblThongTinHocVienService.gettblThongTinHocVien(new Long(hocVienId))); 
        tblThongTinHocVienMonHoc.setMonHocId(tblCTDTMonHocService.gettblCTDTMonHoc(new Long(monHocId))); 
        tblThongTinHocVienMonHoc.setTrangThai(trangThai); 
        preUpdate(msg,tblThongTinHocVienMonHoc); 
       if (msg.isPreMessage_b()) 
       { 
           tblThongTinHocVienMonHocService.updatetblThongTinHocVienMonHoc(tblThongTinHocVienMonHoc); 
           postUpdate(msg, tblThongTinHocVienMonHoc); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 

       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
      tblThongTinHocVienMonHocs =       tblThongTinHocVienMonHocService.findAlltblThongTinHocVienMonHoc(pageSize, page); 
      pages = tblThongTinHocVienMonHocService.counttblThongTinHocVienMonHocByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&mssv="; 
      dataString += "&maMonHoc="; 
      dataString += "&trangThai="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Update tbl_ThongTin_HocVien_MonHoc Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
  public String form() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_ThongTin_HocVien_MonHoc", "xem")){ 
        return "noprivilege"; 
    } 
 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&mssv="; 
      dataString += "&maMonHoc="; 
      dataString += "&trangThai="; 

        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi Form tbl_ThongTin_HocVien_MonHoc Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
  public String delete() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_ThongTin_HocVien_MonHoc", "xoa")){ 
        return "noprivilege"; 
    }           
    
    tblThongTinHocVienMonHocService = (TblThongTinHocVienMonHocService)context.getBean("tbl_ThongTin_HocVien_MonHocClient"); 
    msg = new Message(); 

    List<String> ids = new ArrayList<String>(); 
    Collections.addAll(ids, idList.split(",")); 
    for(String id : ids){ 
        tblThongTinHocVienMonHocService.deleteListtblThongTinHocVienMonHoc(ids); 
    } 
       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
      tblThongTinHocVienMonHocs =       tblThongTinHocVienMonHocService.findAlltblThongTinHocVienMonHoc(pageSize, page); 
      pages = tblThongTinHocVienMonHocService.counttblThongTinHocVienMonHocByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&mssv="; 
      dataString += "&maMonHoc="; 
      dataString += "&trangThai="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi Delete tbl_ThongTin_HocVien_MonHoc Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
 	private int preInsert(Message msg, TblThongTinHocVienMonHoc obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblThongTinHocVienMonHoc obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
	
	
	
	private String mssv;
	public String getMssv() {
		return mssv;
	}

	public void setMssv(String mssv) {
		this.mssv = mssv;
	}

	private String maMonHoc;
 public String getMaMonHoc() {
		return maMonHoc;
	}

	public void setMaMonHoc(String maMonHoc) {
		this.maMonHoc = maMonHoc;
	}

public String insert() throws Exception 
{ 
 try{ 
	 
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext();
    
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
   
    
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_ThongTin_HocVien_MonHoc", "them")){ 
        return "noprivilege"; 
    }           
    tblThongTinHocVienMonHocService = (TblThongTinHocVienMonHocService)context.getBean("tbl_ThongTin_HocVien_MonHocClient"); 
           tblThongTinHocVienService = (TblThongTinHocVienService)context.getBean("tbl_ThongTin_HocVienClient"); 
           tblCTDTMonHocService = (TblCTDTMonHocService)context.getBean("tbl_CTDT_MonHocClient"); 
        
           msg = new Message(); 
        tblThongTinHocVienMonHoc = new TblThongTinHocVienMonHoc(); 
        
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
        {
        	 ConditionReport con = new ConditionReport(); 
             con.setCol("MSSV");
             con.setValue1(mssv);
             con.setOperator("eq");
             cons.add(con);	
        }
        List<TblThongTinHocVien> hocVien = tblThongTinHocVienService.findtblThongTinHocVienByCondition(cons, 1, 1);
        if(hocVien == null || hocVien.size() == 0)
        	return Action.ERROR;
        
        tblThongTinHocVienMonHoc.setHocVienId(hocVien.get(0)); 
        
        cons.clear();
        {
       	 ConditionReport con = new ConditionReport(); 
            con.setCol("maMonHoc");
            con.setValue1(maMonHoc);
            con.setOperator("eq");
            cons.add(con);	
       }
       List<TblCTDTMonHoc> monHoc = tblCTDTMonHocService.findtblCTDTMonHocByCondition(cons, 1, 1);
       if(hocVien == null || hocVien.size() == 0)
    	   return Action.ERROR;
       
        tblThongTinHocVienMonHoc.setMonHocId(monHoc.get(0)); 
        
        
        tblThongTinHocVienMonHoc.setTrangThai(trangThai); 
        preInsert(msg,tblThongTinHocVienMonHoc); 
       if (msg.isPreMessage_b()) 
       { 
           id = tblThongTinHocVienMonHocService.createtblThongTinHocVienMonHoc(tblThongTinHocVienMonHoc); 
           postInsert(msg, tblThongTinHocVienMonHoc); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       tblThongTinHocVienMonHoc.setId(id); 
       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
      tblThongTinHocVienMonHocs =       tblThongTinHocVienMonHocService.findAlltblThongTinHocVienMonHoc(pageSize, page); 
      pages = tblThongTinHocVienMonHocService.counttblThongTinHocVienMonHocByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&mssv="; 
      dataString += "&maMonHoc="; 
      dataString += "&trangThai="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_ThongTin_HocVien_MonHoc Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 	private int preSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
 public String search() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_ThongTin_HocVien_MonHoc", "xem")){ 
        return "noprivilege"; 
    } 
           tblThongTinHocVienMonHocService = (TblThongTinHocVienMonHocService)context.getBean("tbl_ThongTin_HocVien_MonHocClient"); 
        msg = new Message(); 
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
    if(id!= null && !id.equals(0)){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("id");
        con.setValue1(new Long(id));
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(mssv != null && !mssv.equals("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("hocVienId.MSSV");
        con.setValue1("%" + mssv + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
    if(maMonHoc != null && !maMonHoc.equals("") ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("monHocId.ma");
        con.setValue1("%" + maMonHoc + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
 
    
        preSearch(msg, cons); 
       if (msg.isPreMessage_b()) 
       { 
           if(pageSize == 0){ 
               pageSize = 20; 
           } 
            if(page == 0){ 
               page =1; 
           } 
          tblThongTinHocVienMonHocs = tblThongTinHocVienMonHocService.findtblThongTinHocVienMonHocByCondition(cons,pageSize,page); 
           pages = tblThongTinHocVienMonHocService.counttblThongTinHocVienMonHocByCondition(cons)/pageSize + 1; 
           postSearch(msg, cons); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
      dataString = ""; 
      dataString += "&id=" + id; 
      dataString += "&mssv=" + mssv; 
      dataString += "&maMonHoc=" + maMonHoc; 
      dataString += "&trangThai=" + trangThai; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Seach tbl_ThongTin_HocVien_MonHoc Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
  public String editForm() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_ThongTin_HocVien_MonHoc", "sua")){ 
        return "noprivilege"; 
    } 
           tblThongTinHocVienMonHocService = (TblThongTinHocVienMonHocService)context.getBean("tbl_ThongTin_HocVien_MonHocClient"); 
        tblThongTinHocVienMonHoc = tblThongTinHocVienMonHocService.gettblThongTinHocVienMonHoc(new Long(id)); 

        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi EditForm tbl_ThongTin_HocVien_MonHoc Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
 } 
 
